package com.ttsx.core.common.handler;

import cn.hutool.core.exceptions.ValidateException;
import com.ttsx.core.common.exception.TTSXException;
import com.ttsx.core.common.result.Result;
import com.ttsx.core.common.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 *
 * @author wb
 * @since 2025-03-01
 */

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result<?> error(Exception e) {
        log.error("全局异常捕获：{}", e.getMessage(), e);
        return Result.failure(ResultCodeEnum.SERVER_ERROR);
    }


    @ExceptionHandler(ValidateException.class)
    public Result<?> error(ValidateException e) {
        log.error("参数校验异常捕获：{}", e.getMessage(), e);
        return Result.failure().message(e.getMessage());
    }

    @ExceptionHandler(TTSXException.class)
    public Result<?> error(TTSXException e) {
        log.error("天天生鲜异常捕获：{}", e.getMessage(), e);
        return Result.failure().message(e.getMessage());
    }
}
